package com.ll.xml;

import com.ll.exception.UserException;
import com.ll.exception.UserExceptionConstants;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.List;
import java.util.Properties;

/**
 * 解析XML文件
 */
public class ManElementHandler implements ElementHandler {

    private static String hgvs_xpath;

    private static String rs_xpath;

    private static String condition_xpath;

    private static String classification_xpath;

    private static String pubmed_xpath;

    public ManElementHandler() {

    }

    public static void init() {
        try {
            Properties properties = new Properties();
            InputStream filePath = ManElementHandler.class.getClassLoader().getResourceAsStream("clinvar.properties");
            properties.load(filePath);
            hgvs_xpath = (String) properties.get("hgvs_xpath");
            rs_xpath = (String) properties.get("rs_xpath");
            condition_xpath = (String) properties.get("condition_xpath");
            classification_xpath = (String) properties.get("classification_xpath");
            pubmed_xpath = (String) properties.get("pubmed_xpath");
        } catch (IOException e) {
            throw new UserException(UserExceptionConstants.IO_ERROR);
        }

    }

    @Override
    public void onStart(ElementPath elementPath) {
        init();
    }

    @Override
    public void onEnd(ElementPath elementPath) {
        Element row = elementPath.getCurrent();
        Document document = row.getDocument();

        Node HGVS_conding_RefSeq = document.selectSingleNode(hgvs_xpath);
        Element rs = (Element) document.selectSingleNode(rs_xpath);
        Node condition = document.selectSingleNode(condition_xpath);
        Node classification = document.selectSingleNode(classification_xpath);
        @SuppressWarnings("unchecked")
        List<Node> pubmedList = document.selectNodes(pubmed_xpath);
        row.detach();

    }

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        SAXReader saxReader = new SAXReader();
        saxReader.addHandler("/ReleaseSet/ClinVarSet", new
                ManElementHandler());
        Document document = saxReader.read(new BufferedInputStream(new
                FileInputStream(new File("d:/ClinVarFullRelease_2016-10.xml"))));
    }
}
